package cn.itsource.smartcampus.controller;

import cn.itsource.smartcampus.domain.Teacher;
import cn.itsource.smartcampus.service.TeacherService;
import cn.itsource.smartcampus.util.AjaxResult;
import cn.itsource.smartcampus.util.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 针对老师的前端控制器
 */
@RestController
@RequestMapping("/tea")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 查询全部老师的信息
     * @return
     */
    @GetMapping
    public AjaxResult getAll(){
        List<Teacher> all = teacherService.getAll();
        return AjaxResult.ok(all);
    }

    /**
     * 添加老师信息
     * @param tea
     * @return
     */
    @PutMapping
    public AjaxResult insert(@RequestBody Teacher tea){
        AjaxResult<Integer> insert = teacherService.insert(tea);
        return AjaxResult.ok(insert);
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult deleteByPrimaryKey(@PathVariable("id") long id){
        int i = teacherService.deleteByPrimaryKey(id);
        if (i>0) {
            return AjaxResult.ok(i);
        }
        return null;
    }

    /**
     * 修改数据
     * @param tea
     * @return
     */
    @PostMapping
    public AjaxResult updateByPrimaryKeySelective(@RequestBody Teacher tea){
        int i = teacherService.updateByPrimaryKeySelective(tea);
        return AjaxResult.ok(i);
    }

    /**
     * 老师信息导出
     * @param response
     * @throws IOException
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Teacher> list = teacherService.getAll();
        ExcelUtil.exportExcel(list, "智慧校园管理系统-老师信息", "老师信息表", Teacher.class, "Teachers", response);
    }

    /**
     * 老师信息导入模板
     * @param response
     * @throws IOException
     */
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<Teacher> list = new ArrayList<>();
        ExcelUtil.exportExcel(list, "智慧校园管理系统-老师信息", "老师信息表", Teacher.class, "老师信息导入模板", response);
    }

    /**
     * 老师信息导入
     * @param file
     * @return
     */
    @PostMapping("/importExcel")
    public AjaxResult<Void> importExcel(@RequestParam("file") MultipartFile file) {
        teacherService.importExcel(file);
        return AjaxResult.ok("导入成功！");
    }
}
